package org.ssi.dao.impl;

import java.util.HashMap;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.ssi.dao.BaseDao;

import com.bjtech.web.util.SelectBean;


/**
 * iBATIS DAO default implement
 * 
 * @param <T>
 * @version 2.0
 */
public class BaseDaoSqlMapImpl<T> extends SqlMapClientDaoSupport implements BaseDao<T> {

	public static final String PREFIX_INSERT = "insert";

	public static final String PREFIX_UPDATE = "update";

	public static final String PREFIX_DELETE = "delete";

	public static final String PREFIX_SELECT = "select";

	public static final String SUFFIX_COUNT = "Count";

	public static final String SUFFIX_LIST = "List";

	public static final String SUFFIX_PAGINATED_LIST = "PaginatedList";

	public BaseDaoSqlMapImpl() {
		super();
	}

	public int deleteEntity(T t,Long id) throws DataAccessException {
		String statement = new StringBuffer().append(PREFIX_DELETE).append(t.getClass().getSimpleName()).toString();
		return super.getSqlMapClientTemplate().update(statement, id);
	}

	public Long insertEntity(T t) throws DataAccessException {
		String statement = new StringBuffer().append(PREFIX_INSERT).append(t.getClass().getSimpleName()).toString();
		return (Long) super.getSqlMapClientTemplate().insert(statement, t);
	}

	@SuppressWarnings("unchecked")
	public T selectEntity(T t,Long id) throws DataAccessException {
		String statement = new StringBuffer().append(PREFIX_SELECT).append(t.getClass().getSimpleName()).toString();
		return (T) super.getSqlMapClientTemplate().queryForObject(statement, id);
	}

	public Long selectEntityCount(T t,SelectBean sb) throws DataAccessException {
		String statement = new StringBuffer().append(PREFIX_SELECT).append(t.getClass().getSimpleName()).append(
				SUFFIX_COUNT).toString();
		return (Long) super.getSqlMapClientTemplate().queryForObject(statement, sb);
	}

	@SuppressWarnings("unchecked")
	public List<HashMap> selectEntityList(T t,SelectBean sb) throws DataAccessException {
		String statement = new StringBuffer().append(PREFIX_SELECT).append(t.getClass().getSimpleName()).append(
				SUFFIX_LIST).toString();
		return super.getSqlMapClientTemplate().queryForList(statement, sb);
	}

	@SuppressWarnings("unchecked")
	public List<HashMap> selectEntityPaginatedList(T t,SelectBean sb) throws DataAccessException {
		String statement = new StringBuffer().append(PREFIX_SELECT).append(t.getClass().getSimpleName()).append(SUFFIX_PAGINATED_LIST).toString();
		sb.getPager().setAllcol(this.selectEntityCount(t,sb).intValue());
		return super.getSqlMapClientTemplate().queryForList(statement, sb);
	}

	public int updateEntity(T t) throws DataAccessException {
		String statement = new StringBuffer().append(PREFIX_UPDATE).append(t.getClass().getSimpleName()).toString();
		return super.getSqlMapClientTemplate().update(statement, t);
	}
}
